package com.lizhi.rpc.handler;/**
 * @program: hbsf_yun
 * @name RecvTrainingPatientHandler
 * @description:
 * @author: lizhi
 * @create: 2021-10-29 10:17
 */

import com.lizhi.common.yun.entity.RpcClientPOJO;
import com.lizhi.common.yun.entity.RpcPatientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.entity.UpDataPOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.DateUtils;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.cache.MemoryCacheUtil;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.sfrobot.entity.Patient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 *@program: hbsf_yun
 *@name RecvTrainingPatientHandler
 *@description: 正在训练的患者信息
 *@author: lizhi
 *@create: 2021-10-29 10:17
 */
@Slf4j
@Service
public class RecvTrainingPatientHandler {

    public String recvPatient(String json){
        UpDataPOJO upClientPOJO = SamJsonUtil.toObject(json, UpDataPOJO.class);
        RpcClientPOJO rpcClientPOJO = upClientPOJO.getRpcClientPOJO();

        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        rpcServicePOJO.setSystemType(rpcClientPOJO.getSystemType());
        rpcServicePOJO.setOrganId(rpcClientPOJO.getOrganId());
        rpcServicePOJO.setBkOrganId(rpcClientPOJO.getBkOrganId());
        if (StringUtils.isEmpty(rpcClientPOJO)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000001.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000001.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        /*上传的数据信息信息*/
        Patient patient = SamJsonUtil.toObject(upClientPOJO.getData(), Patient.class);
        if (StringUtils.isEmpty(patient)){
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000002.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000002.getEnName());
            return SamJsonUtil.toJson(rpcServicePOJO);
        }

        Patient example = new Patient();
        example.setId(patient.id);
        example.setHospitalId(rpcClientPOJO.getOrganId());
        example.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        /*将医院编号和设备编号放进来，以判断是哪个设备传输的*/
        patient.setCreateTime(DateUtils.getNowDate());
        patient.setHospitalId(rpcClientPOJO.getOrganId());
        patient.setHospitalSystemId(rpcClientPOJO.getBkOrganId());
        log.info("上传的数据信息是patient:{}", patient);
        rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
        rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());

        RpcPatientPOJO rpcPatientPOJO = new RpcPatientPOJO();
        rpcPatientPOJO.setPtId(patient.id);
        rpcPatientPOJO.setPtNm(patient.name);
        rpcPatientPOJO.setPtSexText(patient.genderText);
        rpcPatientPOJO.setPtAge(String.valueOf(patient.age));
        rpcPatientPOJO.setPtPhone(patient.phone);
        rpcPatientPOJO.setHospitalId(patient.getHospitalId());
        rpcPatientPOJO.setHospitalSystemId(patient.getHospitalSystemId());

        /*放入到缓存中*/
        MemoryCacheUtil.put(rpcPatientPOJO.getHospitalId() + "-" + rpcPatientPOJO.getHospitalSystemId(), rpcPatientPOJO, 40000);
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
